﻿#include "PatternDAP_Server.h"

#include <iostream>
#include <ostream>

#include "dap/network.h"

PatternDAP_Server::PatternDAP_Server(int port) : port_(port) {
  // 创建并启动服务端
  const auto server = dap::net::Server::create();
  // 启动服务端,不会阻塞
  server->start(
      port_,
      [this](auto &&PH1) {
        onClientConnected(std::forward<decltype(PH1)>(PH1));
      },
      [](auto &&PH1) {
        PatternDAP_Server::onError(std::forward<decltype(PH1)>(PH1));
      });
}

void PatternDAP_Server::onError(const char *msg) {
  // TODO: pattern调试报错了,错误信息上传到界面
  std::cout << msg << std::endl;
}

void PatternDAP_Server::onClientConnected(
    const std::shared_ptr<dap::ReaderWriter> &socket) {
  // 创建会话
  std::shared_ptr session = dap::Session::create();
  // Set the session to close on invalid data. This ensures that data received over the network
  // receives a baseline level of validation before being processed.
  session->setOnInvalidData(dap::kClose);
  session->bind(socket);

  // 注册回调函数
  initializeRequest(session);
  launchRequest(session);
  setFunctionBreakPointsRequest(session);
  setInstructionBreakpointsRequest(session);
  setExceptionBreakPointsRequest(session);
  setDataBreakpointsRequest(session);
  configurationDoneRequest(session);
  threadsRequest(session);
  stackTraceRequest(session);
  scopesRequest(session);
  variablesRequest(session);
}

void PatternDAP_Server::initializeRequest(
    std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::launchRequest(std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::setFunctionBreakPointsRequest(
    std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::setInstructionBreakpointsRequest(
    std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::setExceptionBreakPointsRequest(
    std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::setDataBreakpointsRequest(
    std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::configurationDoneRequest(
    std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::threadsRequest(std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::stackTraceRequest(
    std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::scopesRequest(std::shared_ptr<dap::Session> session) {
}

void PatternDAP_Server::variablesRequest(
    std::shared_ptr<dap::Session> session) {
}
